/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.VendorInvoiceEntity;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import type.VendorInvoiceStatus;

/**
 *
 * @author thanhle
 */
@Stateless
public class VendorInvoiceEntityFacade extends AbstractFacade<VendorInvoiceEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    //a trick to save the invoice ID
    public VendorInvoiceEntity getTempInvoice() {
        try {
            Query query = (Query) em.createQuery("Select v from VendorInvoice v WHERE v.temp=true");
            VendorInvoiceEntity res = (VendorInvoiceEntity) query.getSingleResult();
            return res;
        } catch (NoResultException e) {
            return null;
        }

    }
    
     //a trick to save the invoice ID
    public List<VendorInvoiceEntity> getSettledInvoice() {
       
        String queryStr = "Select v from VendorInvoice v"
                + " where v.status=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", VendorInvoiceStatus.settled);
        return query.getResultList();
 
    }


    public VendorInvoiceEntityFacade() {
        super(VendorInvoiceEntity.class);
    }
}
